package wendyeq.iweb.blog.dao.impl;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import wendyeq.iweb.blog.dao.ICategoryDAO;
import wendyeq.iweb.blog.pojo.Article;
import wendyeq.iweb.blog.pojo.Category;
import wendyeq.iweb.blog.pojo.Tags;
import wendyeq.iweb.blog.util.JdoManager;

@Repository("categoryDAO")
public class CategoryDAO implements ICategoryDAO{
	private static final Logger logger = Logger.getLogger(CategoryDAO.class);
	
	@Override
	public boolean saveOrUpdate(Category category) {
		PersistenceManager manager = JdoManager.getSession();
		try {
			manager.makePersistent(category);
		} catch (Exception e) {
			logger.error(e);
			return false;
		} finally{
			manager.close();
		}
		return true;
	}
/*	
	@SuppressWarnings("unchecked")
	public void updateCountAdd(String catName){
		PersistenceManager manager = JdoManager.getSession();
		Category category = null;
		try {
			Query query = manager.newQuery(Category.class);
		    query.setFilter("name == catName");
		    query.declareParameters("String catName");
		    List<Category> categorys = (List<Category>) query.execute(catName);
		    if(categorys.size()!=0){
		    category = categorys.get(0);
		    category.setCount(category.getCount()+1);
		    manager.makePersistent(category);
		    }
		} catch (Exception e) {
			logger.error(e);
		} finally {
			manager.close();
		}
	}
	
	@SuppressWarnings("unchecked")
	public void updateCountDec(String catName){
		PersistenceManager manager = JdoManager.getSession();
		Category category = null;
		try {
			Query query = manager.newQuery(Category.class);
		    query.setFilter("name == catName");
		    query.declareParameters("String catName");
		    List<Category> categorys = (List<Category>) query.execute(catName);
		    if(categorys.size()!=0){
		    category = categorys.get(0);
		    category.setCount(category.getCount()-1);
		    manager.makePersistent(category);
		    }
		} catch (Exception e) {
			logger.error(e);
		} finally {
			manager.close();
		}
	}*/
	
	@Override
	public boolean delete(String id) {
		PersistenceManager manager = JdoManager.getSession();
		try {
			Category category = manager.getObjectById(Category.class, id);
			manager.deletePersistent(category);
		} catch (Exception e) {
			logger.error(e);
			return false;
		} finally {
			manager.close();
		}
		return true;
	}

	@Override
	public Category findById(String id) {
		PersistenceManager manager = JdoManager.getSession();
		Category category = null;
		try {
		category = manager.getObjectById(Category.class, id);
		} catch (Exception e) {
			logger.error("findById failure! "+e);
		}  
		return category;
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public Category findByName(String catName) {
		PersistenceManager manager = JdoManager.getSession();
		Category category = null;
		Query query = manager.newQuery(Category.class);
		try {
		    query.setFilter("name == catName");
		    query.declareParameters("String catName");
		    List<Category> categorys = (List<Category>) query.execute(catName);
		    if(categorys.size()!=0){
		    	category = categorys.get(0);
		    }
		} catch (Exception e) {
			logger.error(e);
		} finally{  
			query.closeAll();
		}
		return category;
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public List<Category> findByPage(int min, int max){
		List<Category> categorys = null;
		PersistenceManager manager = JdoManager.getSession();
		try {
			Query query = manager.newQuery(Category.class);
			query.setOrdering("createdTime DESC");
			query.setRange(min, max);
			categorys = (List<Category>) query.execute();
			logger.warn("categorys-size: " + categorys.size());
		} catch (Exception e) {
			logger.error(e);
		}
		return categorys;
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public List<Category> findAll() {
		List<Category> categorys = null;
		PersistenceManager manager = JdoManager.getSession();
		try {
			Query query = manager.newQuery(Category.class);
			query.setOrdering("createdTime DESC");
			categorys = (List<Category>) query.execute();
			logger.warn("categorys-size: " + categorys.size());
		} catch (Exception e) {
			logger.error(e);
		}
		return categorys;
	}
	
}
